//
//  JFTCheckViewController.m
//  JftMergeSDK
//
//  Created by mac on 2017/9/19.
//  Copyright © 2017年 HLZ. All rights reserved.
//

#import "JZMCheckViewController.h"
#import "JZMConfigureHeader.h"
#import "NSData+JZMBase64.h"
#import "JZMDES.h"
#import "NSData+AES.h"
@interface JZMCheckViewController ()
@property (nonatomic,strong) UIImageView *img;
@property (nonatomic,strong) UILabel *lable1;
@property (nonatomic,strong) UILabel *lable2;
@property (nonatomic,strong) UIButton *p_button;
@property (nonatomic,strong) UIButton *button;
@end
@implementation JZMCheckViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.img =[[UIImageView alloc]init];
    self.img.contentMode =  UIViewContentModeScaleAspectFill;
    self.img.image =  [UIImage imageNamed:@"tp"];
    [self.view addSubview:self.img];
    
    self.lable1 =[[UILabel alloc]init];
    self.lable1.text =@"1.如果你已完成，请点击“已完成付款”";
    self.lable1.textAlignment =NSTextAlignmentCenter;
    [self.lable1 setFont:[UIFont systemFontOfSize:15]];
    self.lable1.textColor =[UIColor lightGrayColor];
    [self.view addSubview:self.lable1];
    
    self.lable2 =[[UILabel alloc]init];
    self.lable2.text =@"2.如果你未完成，请点击“未完成付款”";
    self.lable2.textColor =[UIColor lightGrayColor];
    self.lable2.textAlignment =NSTextAlignmentCenter;
    [self.lable2 setFont:[UIFont systemFontOfSize:15]];
    [self.view addSubview:self.lable2];
    
    self.p_button =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.p_button setBackgroundColor:HexRGB(0x4894e8)];
    [self.p_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.p_button setTitle:@"已完成付款" forState:UIControlStateNormal];
    [self.p_button addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.p_button];
    
    self.button =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setBackgroundColor:[UIColor whiteColor]];
    self.button.layer.borderWidth =1;
    self.button.layer.borderColor =HexRGB(0x4894e8).CGColor;
    [self.button setTitleColor:HexRGB(0x4894e8) forState:UIControlStateNormal];
    [self.button setTitle:@"未完成付款" forState:UIControlStateNormal];
    
    [self.button addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationLandscapeLeft) {
        [self viewLeft];
    }
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self viewPortrait];
    }
}

- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeRight || orientation == UIInterfaceOrientationLandscapeLeft) {
        [self viewLeft];
        }
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self viewPortrait];
        }
}

- (void)viewPortrait{
    self.img.frame =CGRectMake((JZMSCREEN_WIDTH-106)/2, 87.5, 106, 106);
    self.lable1.frame =  CGRectMake(30, 106+41+87.5, JZMSCREEN_WIDTH-60, 30);
    self.lable2.frame=CGRectMake(30, 106+41+32+87.5, JZMSCREEN_WIDTH-60, 30);
    self.p_button.frame = CGRectMake(50, 106+41+32+87.5+72,JZMSCREEN_WIDTH-100, 40);
    self.button.frame = CGRectMake(50, 106+41+32+87.5+72+40+18,JZMSCREEN_WIDTH-100, 40);
}
- (void)viewLeft{
    self.img.frame =CGRectMake((JZMSCREEN_WIDTH-106)/2, 30, 106,106);
    self.lable1.frame =  CGRectMake(30,106+50+10, JZMSCREEN_WIDTH-60, 30);
    self.lable2.frame= CGRectMake(30,CGRectGetMaxY(self.lable1.frame), JZMSCREEN_WIDTH-60, 30);
    self.p_button.frame = CGRectMake(50, CGRectGetMaxY(self.lable2.frame) + 20,(JZMSCREEN_WIDTH-120)/2, 40);
    self.p_button.frame = JZMISPad?CGRectMake(50, 106+40+30+40+60*2,(JZMSCREEN_WIDTH-120)/2, 40):CGRectMake(50, CGRectGetMaxY(self.lable2.frame) + 20,(JZMSCREEN_WIDTH-120)/2, 40);
    self.button.frame = JZMISPad?CGRectMake(50+(JZMSCREEN_WIDTH-120)/2+20, 106+40+30+40+60*2,(JZMSCREEN_WIDTH-120)/2, 40):CGRectMake(50+(JZMSCREEN_WIDTH-120)/2+20, CGRectGetMaxY(self.lable2.frame) + 20,(JZMSCREEN_WIDTH-120)/2, 40);;
}
-(void)checkAction{
    __weak typeof(self)weakSelf =self;
    if (!self.jzmCheckToken||[self.jzmCheckToken isEqualToString:@""]) {
        return;
    }
    NSString *urlString = [NSString stringWithFormat:@"%@/SDK/tokenQuery?token=%@",[[NSUserDefaults standardUserDefaults] objectForKey:BASEJFTURLKEY],self.jzmCheckToken];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLSession *sesson = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [sesson dataTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!data ||error) {
             weakSelf.openComplete(@"2");
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf dismissViewControllerAnimated:NO completion:^{
                }];
            });
            return ;
        }
        NSError *jftError;
        NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jftError];
        if ([[dic objectForKey:@"flag"] isEqualToString:@"success"]) {
            if ([[dic objectForKey:@"msg"] isEqualToString:@"0"]) {
                
                weakSelf.openComplete(@"0");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                });
            }
            if ([[dic objectForKey:@"msg"] isEqualToString:@"1"]) {
                weakSelf.openComplete(@"1");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                });
            }
        }else{
            weakSelf.openComplete(@"2");
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf dismissViewControllerAnimated:NO completion:^{
                }];
            });
        }
    }];
    [task resume];
}
@end
